package com.polaris.live.utils

import android.app.NotificationManager
import android.content.Context
import com.blankj.utilcode.util.ActivityUtils
import com.google.firebase.analytics.ktx.analytics
import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.google.firebase.ktx.Firebase
import com.polaris.live.App
import com.polaris.live.common.cacha.CacheUtil
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.manager.CoroutineScopeManager
import com.polaris.live.common.resp.UserResp
import com.polaris.live.floating.FloatingWindowManager
import com.polaris.live.im.manager.ImSessionManager
import com.polaris.live.im.sfs.core.SmartFoxManager
import com.polaris.live.manager.GooglePayManager
import com.polaris.live.manager.GooglePushManager
import com.polaris.live.manager.UserStateManager
import com.polaris.live.ui.game.GameUtils
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import com.polaris.live.utils.zego.LiveEngineManage

/**
 * UserUtils
 *
 * @author Created by lucas on 2023/10/25 19:05
 */
object UserUtils {

    //登录成功
    fun loginSuccess(userInfo: UserResp) {
        UserManager.setUser(userInfo)

        loginInit()
    }

    /**
     * 登录后需要调用
     */
    fun loginInit() {
        if (!UserManager.isLogin()) {
            return
        }

        val userId = UserManager.getUserId()
        FirebaseCrashlytics.getInstance().setUserId(userId.toString())
        Firebase.analytics.setUserId(userId.toString())

        GooglePayManager.login()
        GooglePushManager.login()

        if (!UserManager.getUser().phoneNumber.isNullOrBlank()) {
            CacheUtil.savePhoneNumberShowed()
        }
    }

    /**
     * 退出登录
     */
    fun logout(): Boolean {
        if (!UserManager.isLogin()) {
            return false
        }

        val topActivity = ActivityUtils.getTopActivity()
        val activityList = ActivityUtils.getActivityList()

        UserManager.setUser(UserResp())
        OtherUserCacheUtils.clear()
        SmartFoxManager.reset()
        UserStateManager.reset()
        CoroutineScopeManager.reset()
        ImSessionManager.reset()
        LiveEngineManage.reset()
        GameUtils.release()
        cancelNotification(App.getAppContext())
        FloatingWindowManager.onDestroyed()
        Router.instance.setPath(RoutePath.LOGIN_ACTIVITY).navigation(topActivity)
        if (activityList != null && activityList.isNotEmpty()) {
            activityList.forEach {
                it.finish()
            }
        }

        return true
    }

    private fun cancelNotification(context: Context) {
        try {
            val notificationManager = context
                .getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.cancelAll()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 是否为等级0的用户
     */
    fun UserResp.isLevelZero(): Boolean {
        val richLevel = RichCharmImgUtil.getRichNum(account?.richs ?: 0)
        val charmLevel = RichCharmImgUtil.getCharmNum(account?.charms ?: 0)

        return richLevel <= 0 && charmLevel <= 0
    }
}